﻿Imports System.Data.SqlClient
Imports DTO

Public Class NhaCungCapDAO
    Dim dp As DataProvider
    Public Function LoadNCC() As DataTable
        Dim cn As New DataProvider()
        Return cn.ExecuteQuery("LoadNCC")
    End Function


    Public Function LAYTHEOID(ByVal id As Integer) As NhaCungCapDTO
        Dim cn As New DataProvider()
        Dim para = New SqlParameter("@MaNCC", id)
        Dim dt = cn.ExecuteQuery("LoadNCCTheoID", para)
        Dim ncc As New NhaCungCapDTO()
        If dt.Rows.Count <> 0 Then
            ncc.MaNhaCungCap = Integer.Parse(dt.Rows(0).Item("MaNCC").ToString())
            ncc.TenNCC = dt.Rows(0).Item("TenNCC").ToString()
            ncc.DiaChi = dt.Rows(0).Item("DiaChi")
            ncc.SDT = dt.Rows(0).Item("SDT").ToString()
            ncc.MaLoai = Integer.Parse(dt.Rows(0).Item("MaLoai").ToString())
        End If
        Return ncc
    End Function



    Public Function layTatCa() As IList
        Dim cn As New DataProvider()
        Dim array As New ArrayList
        Dim dt = cn.ExecuteQuery("LayTaTCaNCC")

        If dt.Rows.Count <> 0 Then
            For Each dr As DataRow In dt.Rows
                Dim ncc As New NhaCungCapDTO()
                ncc.MaNhaCungCap = Integer.Parse(dr.Item("MaNCC").ToString())
                ncc.TenNCC = dr.Item("TenNCC").ToString()
                ncc.DiaChi = dr.Item("DiaChi").ToString()
                ncc.SDT = dr.Item("SDT").ToString()
                ncc.NgayCungCap = DateTime.Parse(dr.Item("NgayCungCap").ToString())
                ncc.MaLoai = Integer.Parse(dr.Item("MaLoai").ToString())
                array.Add(ncc)
            Next
        End If
        Return array
    End Function
    Public Sub Them(ByVal ncc As NhaCungCapDTO)

        Dim dt As New DataProvider()
        Dim para As SqlParameter() = New SqlParameter(4) {}
        para(0) = New SqlParameter("@TenNCC", ncc.TenNCC)
        para(1) = New SqlParameter("@DiaChi", ncc.DiaChi)
        para(2) = New SqlParameter("@SDT", ncc.SDT)
        para(3) = New SqlParameter("@NgayCungCap", ncc.NgayCungCap)
        para(4) = New SqlParameter("@MaLoai", ncc.MaLoai)
        dt.ExecuteNonQuery("InsertNCC", para)

    End Sub

    Public Sub Xoa(ByVal ncc As Integer)
        Dim cn As New DataProvider()
        Dim para = New SqlParameter("@MaNCC", ncc)
        cn.ExecuteNonQuery("DeleteNCC", para)
    End Sub

    Function LayTatCaTT() As DataTable
        Dim cn As New DataProvider()
        Return cn.ExecuteQuery("LoadNCC")
    End Function

    Sub CapNhap(ByVal ncc As NhaCungCapDTO)

        Dim dp As New DataProvider()
        Dim para As SqlParameter() = New SqlParameter(5) {}
        para(0) = New SqlParameter("@MaNCC", ncc.MaNhaCungCap)
        para(1) = New SqlParameter("@TenNCC", ncc.TenNCC)
        para(2) = New SqlParameter("@DiaChi", ncc.DiaChi)
        para(3) = New SqlParameter("@SDT", ncc.SDT)
        para(4) = New SqlParameter("@NgayCungCap", ncc.NgayCungCap)
        para(5) = New SqlParameter("@MaLoai", ncc.MaLoai)
        dp.ExecuteNonQuery("CapNhapNCC", para)

    End Sub

End Class
